package com.itany.corejava.day13;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TestMouseEvent {

    private JFrame jf;
    private JLabel jl;
    private JTextArea jta;

    public TestMouseEvent() {
        jf=new JFrame();
        jl=new JLabel();
        jta=new JTextArea(20,40);
    }

    private void init(){
        JScrollPane jsp=new JScrollPane(jta);
        jf.add(jl, BorderLayout.NORTH);
        jf.add(jsp);
    }

    private void setSytle(){
        jta.setFont(new Font("宋体",Font.BOLD,26));
        jta.setForeground(Color.RED);
    }

    private void addEventHandler(){
        // jta.addMouseListener(new MouseListener() {
        //     @Override
        //     public void mouseClicked(MouseEvent e) {
        //         String keyName="";
        //         switch (e.getButton()){
        //             case MouseEvent.BUTTON1:
        //                 keyName="左";
        //                 break;
        //             case MouseEvent.BUTTON2:
        //                 keyName="中";
        //                 break;
        //             case MouseEvent.BUTTON3:
        //                 keyName="右";
        //         }
        //         jta.append(keyName+"键被点击"+"("+e.getX()+","+e.getY()+")"+"\n");
        //     }
        //
        //     @Override
        //     public void mousePressed(MouseEvent e) {
        //
        //     }
        //
        //     @Override
        //     public void mouseReleased(MouseEvent e) {
        //
        //     }
        //
        //     @Override
        //     public void mouseEntered(MouseEvent e) {
        //         jta.append("鼠标进入\n");
        //     }
        //
        //     @Override
        //     public void mouseExited(MouseEvent e) {
        //         jta.append("鼠标移出\n");
        //     }
        // });

        jta.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String keyName="";
                switch (e.getButton()){
                    case MouseEvent.BUTTON1:
                        keyName="左";
                        break;
                    case MouseEvent.BUTTON2:
                        keyName="中";
                        break;
                    case MouseEvent.BUTTON3:
                        keyName="右";
                }
                jta.append(keyName+"键被点击"+"("+e.getX()+","+e.getY()+")"+"\n");
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                jta.append("鼠标进入\n");
            }

            @Override
            public void mouseExited(MouseEvent e) {
                jta.append("鼠标移出\n");
            }
        });

        jta.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                jl.setText("("+e.getX()+","+e.getY()+")");
            }
        });

    }

    public void show(){
        init();
        setSytle();
        addEventHandler();
        jf.pack();
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestMouseEvent().show();
    }
}
